﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuestionType = Zebra.Umbraco.Marcos.Code.QuestionType;

namespace Zebra.Umbraco.Marcos.Survey.QuestionEditor
{
    public partial class TextQuestion : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            if (Request.HttpMethod.ToLower() == "post")
            {
                if (Request["QuestionID"] == null)
                {
                    if (Request["Question_Title"] != null
                      && Request["Question_MustAnswer"] != null
                      && Request["Question_RowCount"] != null
                      && Request["Question_Width"] != null
                       && Request["SurveyID"] != null
                      )
                    {
                        QuestionType.CommentQuestion question = new QuestionType.CommentQuestion()
                        {
                            QuestionText = Request["Question_Title"],
                        };
                        bool tryBoolValue;
                        int tryIntValue;
                        if (bool.TryParse(Request["Question_MustAnswer"], out tryBoolValue))
                        {
                            question.MustAnswer = tryBoolValue;
                        }
                        if (int.TryParse(Request["Question_Width"], out tryIntValue))
                        {
                            question.Width = tryIntValue;
                        }
                        if (int.TryParse(Request["Question_RowCount"], out tryIntValue))
                        {
                            question.Height = tryIntValue;
                        }
                        using (DataClasses1DataContext db = new DataClasses1DataContext())
                        {
                            surveyQuestion sQuestion = new surveyQuestion()
                            {
                                questionXML = question.ToXml(),
                                surveyID = int.Parse(Request["SurveyID"]),
                                questionType = Enum.GetName(typeof(QuestionType.QuestionCatalog), question.Catalog),
                                surveyPage = 1,
                                RowCreatedOn = DateTime.Now,
                                RowModifiedOn = DateTime.Now,
                            };
                            db.surveyQuestions.InsertOnSubmit(sQuestion);
                            db.SubmitChanges();
                            question.QuestionID = sQuestion.surveyQuestionID;
                            sQuestion.questionXML = question.ToXml();
                            db.SubmitChanges();
                        }
                        Response.Write(question.GenerateHTML(true));
                        Response.End();
                    }
                }
                else
                {
                    if (Request["Question_Title"] != null
                      && Request["Question_MustAnswer"] != null
                      && Request["Question_RowCount"] != null
                      && Request["Question_Width"] != null
                       && Request["SurveyID"] != null
                      )
                    {
                        try
                        {
                            using (DataClasses1DataContext db = new DataClasses1DataContext())
                            {
                                surveyQuestion sQuestion = db.surveyQuestions.First(c => c.surveyID == int.Parse(Request["SurveyID"]) && c.surveyQuestionID == int.Parse(Request["QuestionID"]));
                                QuestionType.CommentQuestion question = (QuestionType.CommentQuestion)QuestionType.Question.FromXML(sQuestion.questionType, sQuestion.questionXML);

                                question.QuestionText = Request["Question_Title"];
                                bool tryBoolValue;
                                int tryIntValue;
                                if (bool.TryParse(Request["Question_MustAnswer"], out tryBoolValue))
                                {
                                    question.MustAnswer = tryBoolValue;
                                }
                                if (int.TryParse(Request["Question_Width"], out tryIntValue))
                                {
                                    question.Width = tryIntValue;
                                }
                                if (int.TryParse(Request["Question_RowCount"], out tryIntValue))
                                {
                                    question.Height = tryIntValue;
                                }
                                sQuestion.questionXML = question.ToXml();
                                sQuestion.RowModifiedOn = DateTime.Now;
                                db.SubmitChanges();
                                Response.Write(question.GenerateHTML(true));
                            }

                        }
                        catch (Exception ex)
                        {
                            Response.Write(ex.Message);
                            Response.Write(ex.StackTrace);
                        }
                        Response.End();
                    }
                }
            }
            else
            {
                if (Request["QuestionID"] != null)
                {
                    if (Request["Delete"] != null)
                    {

                        using (DataClasses1DataContext db = new DataClasses1DataContext())
                        {
                            surveyQuestion sQuestion = db.surveyQuestions.First(c => c.surveyID == int.Parse(Request["SurveyID"]) && c.surveyQuestionID == int.Parse(Request["QuestionID"]));
                            db.surveyQuestions.DeleteOnSubmit(sQuestion);
                            db.SubmitChanges();

                        }
                        Response.End();
                    }
                    else
                    {

                        using (DataClasses1DataContext db = new DataClasses1DataContext())
                        {
                            surveyQuestion sQuestion = db.surveyQuestions.First(c => c.surveyID == int.Parse(Request["SurveyID"]) && c.surveyQuestionID == int.Parse(Request["QuestionID"]));
                            QuestionType.CommentQuestion question = (QuestionType.CommentQuestion)QuestionType.Question.FromXML(sQuestion.questionType, sQuestion.questionXML);
                            this.Question_Title.InnerText = question.QuestionText;
                            this.Question_MustAnswer.Checked = question.MustAnswer;
                            this.Question_RowCount.SelectedIndex = question.Height - 2;
                            this.Question_Width.SelectedIndex = question.Width / 10 - 1;
                        }
                    }
                }
            }
        }
    }
}